package css.sword.auth;

import css.sword.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 用户身份认证信息存放在redis中
 */

@Component
public class AuthInterceptor implements HandlerInterceptor {

    /**
     * 日志
     */
    Logger log = LoggerFactory.getLogger(AuthInterceptor.class);

    /**
     * redis 注入
     */
    @Autowired
    private css.sword.util.RedisDao redisUtil;

    @Resource(name = "redisSecondary")
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getParameter("token");
        String identityId = request.getParameter("identityId");
        String staticToken = request.getParameter("staticToken");
        log.info("token=" + token + ",identitiyId=" + identityId + ",staticToken=" + staticToken);
        if (StringUtils.isNotEmpty(staticToken) && staticToken.equals("f1634ed30b374f159a684ef353184f16")) {
            return true;
        }
        if (StringUtils.isEmpty(identityId) || StringUtils.isEmpty(token))
            return false;
        String redisToken = "";
        String redisToken1 = "";
        try {
            redisToken = redisUtil.getString("String:fileAllow:identityId:" + identityId);
            //`` factory=(LettuceConnectionFactory)redisTemplate.getConnectionFactory();
            if (redisTemplate.getConnectionFactory() != null)
                redisToken1 = redisTemplate.opsForValue().get("String:fileAllow:identityId:" + identityId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (redisToken == null)
            redisToken = "";
        if (redisToken1 == null)
            redisToken1 = "";
        if (StringUtils.isEmpty(redisToken) && StringUtils.isEmpty(staticToken) && StringUtils.isEmpty(redisToken1)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "====用户权限验证失败====");
            return false;
        }
        if ((StringUtils.isNotEmpty(identityId) && StringUtils.isNotEmpty(token) && (redisToken.equals(token) || redisToken1.equals(token)))) {
            return true;
        } else {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "====用户权限验证失败====");
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        log.info("--------------处理请求完成后视图渲染之前的处理操作---------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        log.info("---------------视图渲染之后的操作-------------------------0");
    }
}
